/**
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via
 * e-mail: ralf.vandenhouten@th-wildau.de.
 */
package de.thwildau.tm.moses.asn1.generator;

import com.google.common.base.Objects;
import de.thwildau.tm.moses.asn1.util.generator.GenConstraint;
import de.thwildau.tm.moses.asn1.util.generator.GenSizeConstraint;
import de.thwildau.tm.moses.asn1.util.generator.GenValueRangeConstraint;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.emf.codegen.util.ImportManager;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.StringExtensions;

@SuppressWarnings("all")
public class GeneratorExtensions {
  public static String getImport(final ImportManager im, final String qualifiedName) {
    return im.getImportedName(qualifiedName, true);
  }
  
  public static <T extends Object> void forEach(final Iterable<T> iter, final Function2<? super T,? super Integer,? extends Object> handler) {
    AtomicInteger _atomicInteger = new AtomicInteger(0);
    final AtomicInteger i = _atomicInteger;
    final Procedure1<T> _function = new Procedure1<T>() {
        public void apply(final T item) {
          int _andAdd = i.getAndAdd(1);
          handler.apply(item, Integer.valueOf(_andAdd));
        }
      };
    IterableExtensions.<T>forEach(iter, _function);
  }
  
  public static CharSequence enumFieldDelimiter(final boolean isLastField) {
    StringConcatenation _builder = new StringConcatenation();
    {
      if (isLastField) {
        _builder.append(";");
      } else {
        _builder.append(",");
      }
    }
    return _builder;
  }
  
  public static CharSequence toLong(final Long value) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append(value, "");
    {
      boolean _or = false;
      boolean _greaterThan = ((value).longValue() > Integer.MAX_VALUE);
      if (_greaterThan) {
        _or = true;
      } else {
        boolean _lessThan = ((value).longValue() < Integer.MIN_VALUE);
        _or = (_greaterThan || _lessThan);
      }
      if (_or) {
        _builder.append("L");
      }
    }
    return _builder;
  }
  
  public static CharSequence toSetterGetterMethod(final String type, final String fieldname) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _setterMethod = GeneratorExtensions.toSetterMethod(type, fieldname);
    _builder.append(_setterMethod, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    CharSequence _getterMethod = GeneratorExtensions.toGetterMethod(type, fieldname);
    _builder.append(_getterMethod, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  public static CharSequence toSetterGetterMethod(final String type, final String fieldname, final GenConstraint<Long> constraint, final ImportManager im) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _setterMethod = GeneratorExtensions.toSetterMethod(type, fieldname, constraint, im);
    _builder.append(_setterMethod, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    CharSequence _getterMethod = GeneratorExtensions.toGetterMethod(type, fieldname);
    _builder.append(_getterMethod, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  public static CharSequence toGetterMethod(final String type, final String fieldname) {
    StringConcatenation _builder = new StringConcatenation();
    String methodSuffix = StringExtensions.toFirstUpper(fieldname);
    _builder.newLineIfNotEmpty();
    _builder.append("public ");
    _builder.append(type, "");
    _builder.append(" get");
    _builder.append(methodSuffix, "");
    _builder.append("() {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("return ");
    _builder.append(fieldname, "	");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  public static CharSequence toSetterMethod(final String type, final String fieldname) {
    StringConcatenation _builder = new StringConcatenation();
    String methodSuffix = StringExtensions.toFirstUpper(fieldname);
    _builder.newLineIfNotEmpty();
    _builder.append("public void set");
    _builder.append(methodSuffix, "");
    _builder.append("(");
    _builder.append(type, "");
    _builder.append(" ");
    _builder.append(fieldname, "");
    _builder.append(") {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("this.");
    _builder.append(fieldname, "	");
    _builder.append(" = ");
    _builder.append(fieldname, "	");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  public static CharSequence toSetterMethod(final String type, final String fieldname, final GenConstraint<Long> constraint, final ImportManager im) {
    StringConcatenation _builder = new StringConcatenation();
    String methodSuffix = StringExtensions.toFirstUpper(fieldname);
    _builder.newLineIfNotEmpty();
    _builder.append("public void set");
    _builder.append(methodSuffix, "");
    _builder.append("(");
    _builder.append(type, "");
    _builder.append(" ");
    _builder.append(fieldname, "");
    _builder.append(") {");
    _builder.newLineIfNotEmpty();
    {
      boolean _notEquals = (!Objects.equal(constraint, null));
      if (_notEquals) {
        _builder.append("\t");
        String _importedName = im.getImportedName("de.thwildau.tm.moses.asn1.lang.Validator", true);
        _builder.append(_importedName, "	");
        _builder.append(".");
        String _validatorMethodName = GeneratorExtensions.getValidatorMethodName(constraint);
        _builder.append(_validatorMethodName, "	");
        _builder.append("(getClass(), \"");
        _builder.append(fieldname, "	");
        _builder.append("\", ");
        _builder.append(fieldname, "	");
        _builder.append(");");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("\t");
    _builder.append("this.");
    _builder.append(fieldname, "	");
    _builder.append(" = ");
    _builder.append(fieldname, "	");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  private static String getValidatorMethodName(final GenConstraint<Long> constraint) {
    String _switchResult = null;
    boolean _matched = false;
    if (!_matched) {
      if (constraint instanceof GenSizeConstraint) {
        final GenSizeConstraint<Long> _genSizeConstraint = (GenSizeConstraint<Long>)constraint;
        _matched=true;
        return "checkSizeConstraint";
      }
    }
    if (!_matched) {
      if (constraint instanceof GenValueRangeConstraint) {
        final GenValueRangeConstraint<Long> _genValueRangeConstraint = (GenValueRangeConstraint<Long>)constraint;
        _matched=true;
        return "checkRangeConstraint";
      }
    }
    return _switchResult;
  }
}
